/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.authorization.domain.SysRoleMember;
import cn.ibizlab.core.authorization.service.SysRoleMemberService;
import cn.ibizlab.core.authorization.filter.SysRoleMemberSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysRoleMember] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysRoleMemberResource {

    @Autowired
    public SysRoleMemberService sysRoleMemberService;

    @Autowired
    @Lazy
    public SysRoleMemberDTOMapping sysRoleMemberDtoMapping;

    @Autowired
    @Lazy
    public SysRoleMemberFilterDTOMapping sysRoleMemberFilterDtoMapping;


    /**
    * 创建Create 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleMemberDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"系统角色成员" },  notes = "SysRoleMember-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Create-all') or hasPermission(this.sysRoleMemberDtoMapping.toDomain(#dto),'uaa-SysRoleMember-Create')")
    @PostMapping(value = {"sys_role_members","sys_role_members"})
    public ResponseEntity<SysRoleMemberDTO> create
            (@Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        sysRoleMemberService.create(domain);
        SysRoleMember rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 系统角色成员
    * 
    *
    * @param id id
    * @return ResponseEntity<SysRoleMemberDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"系统角色成员" },  notes = "SysRoleMember-Get ")
    @GetMapping("sys_role_members/{id}")
    public ResponseEntity<SysRoleMemberDTO> getById
            (@PathVariable("id") String id) {
        SysRoleMember rt = sysRoleMemberService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 系统角色成员
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"系统角色成员" },  notes = "SysRoleMember-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Remove-all') or hasPermission(this.sysRoleMemberService.get(#id),'uaa-SysRoleMember-Remove')")
    @DeleteMapping("sys_role_members/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysRoleMemberService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 系统角色成员
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysRoleMemberDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"系统角色成员" },  notes = "SysRoleMember-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Update-all') or hasPermission(this.sysRoleMemberService.get(#id),'uaa-SysRoleMember-Update')")
    @VersionCheck(entity = "sysrolemember" , versionfield = "updateTime")
    @PutMapping("sys_role_members/{id}")
    public ResponseEntity<SysRoleMemberDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        domain.setId(id);
        sysRoleMemberService.update(domain);
        SysRoleMember rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"系统角色成员" },  notes = "SysRoleMember-CheckKey ")
    @PostMapping("sys_role_members/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        Integer rt = sysRoleMemberService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleMemberDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"系统角色成员" },  notes = "SysRoleMember-GetDraft ")
    @GetMapping("sys_role_members/getdraft")
    public ResponseEntity<SysRoleMemberDTO> getDraft
            (@SpringQueryMap SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        SysRoleMember rt = sysRoleMemberService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * Member 系统角色成员
    * 根据传入role_id系统角色标识查询人员清单，可选过滤参数有: department_id，organization_id，member_group_id，member_name，member_uid
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "Member", tags = {"系统角色成员" },  notes = "SysRoleMember-Member 根据传入role_id系统角色标识查询人员清单，可选过滤参数有: department_id，organization_id，member_group_id，member_name，member_uid")
    @PostMapping("sys_role_members/{id}/member")
    public ResponseEntity<List<SysRoleMemberDTO>> memberById
            (@PathVariable("id") String id, @Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysRoleMember> rt = sysRoleMemberService.member(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleMemberDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"系统角色成员" },  notes = "SysRoleMember-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Save-all') or hasPermission(this.sysRoleMemberDtoMapping.toDomain(#dto),'uaa-SysRoleMember-Save')")
    @PostMapping("sys_role_members/save")
    public ResponseEntity<SysRoleMemberDTO> save
            (@Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        sysRoleMemberService.save(domain);
        SysRoleMember rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * ListByRoleIds 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "ListByRoleIds", tags = {"系统角色成员" },  notes = "SysRoleMember-ListByRoleIds ")
    @PostMapping("sys_role_members/listbyroleids")
    public ResponseEntity<List<SysRoleMemberDTO>> listByRoleIds
            (@Validated @RequestBody SysRoleMemberDTO dto) {
        SysRoleMember domain = sysRoleMemberDtoMapping.toDomain(dto);
        List<SysRoleMember> rt = sysRoleMemberService.listByRoleIds(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-FetchDefault-all') or hasPermission(#dto,'uaa-SysRoleMember-FetchDefault')")
    @PostMapping("sys_role_members/fetchdefault")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchDefault
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchDefault(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchNav 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchNav", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchNav ")
    @PostMapping("sys_role_members/fetchnav")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchNav
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchNav(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchRoleByGroupId 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchRoleByGroupId", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchRoleByGroupId ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-FetchRoleByGroupId-all') or hasPermission(#dto,'uaa-SysRoleMember-FetchRoleByGroupId')")
    @PostMapping("sys_role_members/fetchrolebygroupid")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchRoleByGroupId
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchRoleByGroupId(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchRoleByPersonId 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchRoleByPersonId", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchRoleByPersonId ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-FetchRoleByPersonId-all') or hasPermission(#dto,'uaa-SysRoleMember-FetchRoleByPersonId')")
    @PostMapping("sys_role_members/fetchrolebypersonid")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchRoleByPersonId
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchRoleByPersonId(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchRolesByUserId 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchRolesByUserId", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchRolesByUserId ")
    @PostMapping("sys_role_members/fetchrolesbyuserid")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchRolesByUserId
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchRolesByUserId(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchUsersByRoleId 系统角色成员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleMemberDTO>>
    */
    @ApiOperation(value = "查询FetchUsersByRoleId", tags = {"系统角色成员" },  notes = "SysRoleMember-FetchUsersByRoleId ")
    @PostMapping("sys_role_members/fetchusersbyroleid")
    public ResponseEntity<List<SysRoleMemberDTO>> fetchUsersByRoleId
            (@Validated @RequestBody SysRoleMemberFilterDTO dto) {
        SysRoleMemberSearchContext context = sysRoleMemberFilterDtoMapping.toDomain(dto);
        Page<SysRoleMember> domains = sysRoleMemberService.searchUsersByRoleId(context) ;
        List<SysRoleMemberDTO> list = sysRoleMemberDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建系统角色成员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Create-all')")
    @ApiOperation(value = "批量新建系统角色成员", tags = {"系统角色成员" },  notes = "批量新建系统角色成员")
	@PostMapping("sys_role_members/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysRoleMemberDTO> dtos) {
        sysRoleMemberService.createBatch(sysRoleMemberDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除系统角色成员
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Remove-all')")
    @ApiOperation(value = "批量删除系统角色成员", tags = {"系统角色成员" },  notes = "批量删除系统角色成员")
	@DeleteMapping("sys_role_members/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysRoleMemberService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新系统角色成员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Update-all')")
    @ApiOperation(value = "批量更新系统角色成员", tags = {"系统角色成员" },  notes = "批量更新系统角色成员")
	@PutMapping("sys_role_members/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysRoleMemberDTO> dtos) {
        sysRoleMemberService.updateBatch(sysRoleMemberDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存系统角色成员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Save-all')")
    @ApiOperation(value = "批量保存系统角色成员", tags = {"系统角色成员" },  notes = "批量保存系统角色成员")
	@PostMapping("sys_role_members/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysRoleMemberDTO> dtos) {
        sysRoleMemberService.saveBatch(sysRoleMemberDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入系统角色成员
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRoleMember-Save-all')")
    @ApiOperation(value = "批量导入系统角色成员", tags = {"系统角色成员" },  notes = "批量导入系统角色成员")
	@PostMapping("sys_role_members/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysRoleMemberDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysRoleMemberService.importData(config,ignoreError,sysRoleMemberDtoMapping.toDomain(dtos)));
    }

}
